package com.it.service.Impl;

import com.it.mapper.ConfigMapper;
import com.it.mapper.StudentMapper;
import com.it.pojo.*;
import com.it.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper mapper;
    @Autowired
    private ConfigMapper configMapper;

    @Override
    public Admin login(String username, String password) {
        return mapper.login(username, password);
    }

    @Override
    public List<Student> getAll(Integer cid, Integer tid) {
        return mapper.selectByCondition(cid, tid);
    }

    @Override
    public void add(Student s) {
        mapper.add(s);
    }

    @Override
    public void delete(Integer id) {
        mapper.delete(id);
    }

    @Override
    public void update(Student s) {
        mapper.update(s);
    }

    @Override
    public void grade(Student student, boolean plus) {
        Config config = configMapper.getConfig();

        Integer tid = student.getTid();
        List<Student> students = mapper.selectByCondition(null, tid);

        for (Student stu : students) {
            if (stu.getId().equals(student.getId())) {
                if (plus)
                    stu.setGrade(stu.getGrade() + config.getPlus());
                else
                    stu.setGrade(stu.getGrade() - config.getMinus());
            } else {
                if (plus)
                    stu.setGrade(stu.getGrade() + config.getAdd());
                else
                    stu.setGrade(stu.getGrade() - config.getCut());
            }
            mapper.update(stu);
        }

    }
}
